import sys
from threading import Thread

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QDialog

import ui_mainWindow
import ui_success1
import ui_failed1

from ListenerAndEventManager import myEventManager,Event
from rbac import RBAC

# global subWindow,subWindow1
# =============================EVENT FUNCTION=============================================
class EventQianghua1Times(Event):
    def __init__(self,user):
        super(EventQianghua1Times, self).__init__("强化一次",{"data":user})

class EventQianghua10Times(Event):
    def __init__(self,user):
        super(EventQianghua10Times, self).__init__("强化十次",{"data":user})

class EventQianghua100Times(Event):
    def __init__(self,user):
        super(EventQianghua100Times, self).__init__("强化100次",{"data":user})
# =========================================================================================

class rbac_controller(object):
    def __init__(self):

        ##================RBAC INIT===================================
        print("Init RBAC Controller")
        user_list=[]
        for level in range(100):
            for vip in [0,1]:
                user_list.append(str(level+1)+"_"+str(vip))

        role_list=["level_under_10","level_up_20", "vip"]

        user_role_matrix=[]
        for user in user_list:
            level=int(user.split("_")[0])
            is_vip=int(user.split("_")[1])
            alist=[]
            if level<10:
                alist=[1,0]
            elif level>=20:
                alist=[0,1]
            else:
                alist=[0,0]
            if is_vip==1:
                alist.append(1)
            else:
                alist.append(0)
            user_role_matrix.append(alist)

        action_list=[successQiangHua1times, successQiangHua10times, successQiangHua100times]
        cannot_action_list=[cannot_QiangHua1times, cannot_QiangHua10times, cannot_QiangHua100times]

        role_action_matrix=[[1,0,0],[1,1,0],[1,1,1]]

        self.myRBAC=RBAC(user_list,role_list,action_list,cannot_action_list,user_role_matrix,role_action_matrix)
        self.myRBAC.save_json("./config.json")
        print("=============RBAC construct down. SAVE config file to `./config.json`===================")


def successQiangHua1times(objectName):
    objectName.attackPlus1()
    subWindow.show()
    # threads=Thread(target=showSuccessDialog)
    # threads.start()

def successQiangHua10times(objectName):
    for _ in range(10):
        objectName.attackPlus1()
    subWindow.show()

def successQiangHua100times(objectName):
    for _ in range(100):
        objectName.attackPlus1()
    subWindow.show()

def cannot_QiangHua1times(objectName):
    subWindow1.show()

def cannot_QiangHua10times(objectName):
    subWindow1.show()

def cannot_QiangHua100times(objectName):
    subWindow1.show()

global controller

def ListernerQiangHua1times(event1times):
    action=controller.myRBAC.return_actions(event1times.datadict["data"].getUser(),successQiangHua1times)
    action(event1times.datadict["data"])

def ListernerQiangHua10times(event10times):
    action=controller.myRBAC.return_actions(event10times.datadict["data"].getUser(),successQiangHua10times)
    action(event10times.datadict["data"])

def ListernerQiangHua100times(event100times):
    action=controller.myRBAC.return_actions(event100times.datadict["data"].getUser(),successQiangHua100times)
    action(event100times.datadict["data"])


class ZhuangBei(object):
    def __init__(self,id,level,is_vip):

        print("Init states...")
        ## 逻辑变量
        self.userID=id
        self.level=level
        self.is_vip=is_vip
        self.attack=10


    def getUserID(self):
        return self.userID

    def getAttackNum(self):
        return self.attack

    def getLevel(self):
        return self.level

    def getIsVIP(self):
        return self.is_vip

    def setLevel(self,level):
        self.level=level

    def setVIP(self,isornot):
        self.is_vip=isornot

    def getUser(self):
        return str(self.getLevel())+"_"+str(self.getIsVIP())
    def attackPlus1(self):
        self.attack+=1
        print("update! to: {}".format(self.attack))

def queryQiangHua1times(player,mymanager):
    mymanager.sendEvent(EventQianghua1Times(player))

def queryQiangHua10times(player,mymanager):
    mymanager.sendEvent(EventQianghua10Times(player))

def queryQiangHua100times(player,mymanager):
    mymanager.sendEvent(EventQianghua100Times(player))

def mytest1(liangzi):
    # subWindow.show()
    liangzi.attackPlus1()

def mytest2(liangzi):
    liangzi.attackPlus1()
    subWindow.show()
    print("lalala")



if __name__ == '__main__':
    controller=rbac_controller()
    liangzi=ZhuangBei(id="liangzi",level=3,is_vip=0)
    # # liangzi.attackPlus1()
    print(liangzi.getAttackNum())
    mytest1(liangzi)
    print(liangzi.getAttackNum())
    lz=lambda liangzi:mytest1(liangzi)
    lz(liangzi)
    print(liangzi.getAttackNum())

    mymanager=myEventManager()
    mymanager.addEventListerner("强化一次",ListernerQiangHua1times)
    mymanager.addEventListerner("强化十次",ListernerQiangHua10times)
    mymanager.addEventListerner("强化100次",ListernerQiangHua100times)
    mymanager.start()

    ##################################################### GUI变量
    app = QApplication(sys.argv)
    #------------------sub window
    success1=ui_success1.Ui_Dialog_success1()
    subWindow=QWidget()
    # subWindow=QMainWindow()
    success1.setupUi(subWindow)
    success1.pushButton.clicked.connect(subWindow.close)

    fail1=ui_failed1.Ui_Dialog()
    subWindow1=QWidget()
    fail1.setupUi(subWindow1)
    fail1.pushButton.clicked.connect(subWindow1.close)
    #--------------main window
    MainWindow = QMainWindow()
    ui = ui_mainWindow.Ui_mygameframe()
    ui.setupUi(MainWindow)
    print("Begin to show the GUI")
    print("Setting Button Actions...")
    MainWindow.show()
    # ui.pushButton_1time.clicked.connect(lambda liangzi:mytest1(liangzi))
    # ui.pushButton_1time.clicked.connect(lambda:mytest2(liangzi))
    ui.pushButton_1time.clicked.connect(lambda:queryQiangHua1times(liangzi,mymanager))
    ui.pushButton_10time.clicked.connect(lambda:queryQiangHua10times(liangzi,mymanager))
    ui.pushButton_100time.clicked.connect(lambda:queryQiangHua100times(liangzi,mymanager))
    subWindow.show()
    subWindow.close()
    subWindow1.show()
    subWindow1.close()
    print("Now we are listerning the action of buttons....")

    sys.exit(app.exec_())


